package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Admin;
import com.atguigu.entity.Permission;
import com.atguigu.entity.Role;
import com.atguigu.service.AdminRoleService;
import com.atguigu.service.AdminService;
import com.atguigu.service.PermissionService;
import com.atguigu.service.RoleService;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * @Author: 翟文海
 * @Date: 2022/11/4/004 18:31
 * @Version: 1.0
 * @Description:
 **/
@Controller
public class IndexController extends BaseController {
    private static final String PAGE_INDEX = "frame/index";
    @Reference
    private AdminService adminService;
    @Reference
    private RoleService roleService;
    @Reference
    private AdminRoleService adminRoleService;
    @Reference
    private PermissionService permissionService;

    @RequestMapping("/")
    public String index(Model model) {
        //TODO 用户写死为1L
        //获取当前登录的用户
        Object o = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if (o instanceof User) {
            User user = (User) o;
            String username = user.getUsername();
            //1.用户信息admin
            Admin admin = adminService.getByUsername(username);
            model.addAttribute("admin", admin);
            //2.用户角色列表roleList
            List<Role> roleList = roleService.findByAdminId(admin.getId());
            model.addAttribute("roleList", roleList);
            //3.菜单列表permissionList
            List<Permission> permissionList = permissionService.findMenu(admin.getId());
            model.addAttribute("permissionList", permissionList);
        }
        return toPage(PAGE_INDEX);
    }
}
